//
//  DetailViewController.swift
//  swift3.0 小项目练习
//
//  Created by 龚昕炜 on 16/7/27.
//  Copyright © 2016年 龚昕炜. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController {

    @IBOutlet weak var idTextField: UITextField!
    @IBOutlet weak var pwdTextField: UITextField!
    @IBOutlet weak var nameTextField: UITextField!
    
    var person: Person?
    
    /** 返回值可选的闭包 */
//    var complitionCallBack: ()->()?
    /** 闭包是可选的 */
    var complitionCallBack: (()->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white()
        
        if person != nil {
        
            idTextField.text = person?.phone
            pwdTextField.text = person?.title
            nameTextField.text = person?.name
        }
    }
    
    @IBAction func save(_ sender: UIBarButtonItem) {
        
        if person == nil {
            person = Person()
        }
        
        person?.name = nameTextField.text ?? "0"
        person?.title = pwdTextField.text ?? "0"
        person?.phone = idTextField.text ?? "0"
        
        _ = navigationController?.popViewController(animated: true)
        
        /** 此处尽量使用？，如果使用系统提示的!当没有赋值的时候会直接崩溃 */
        complitionCallBack?()
        
    }

}
